package com.tansuo365.test1.service.otherservice.impl;

import com.tansuo365.test1.bean.otherbean.Menu;
import com.tansuo365.test1.mapper.othermapper.MenuMapper;
import com.tansuo365.test1.service.otherservice.MenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class MenuServiceImpl implements MenuService {

    @Resource
    private MenuMapper menuMapper;

    @Override
    public int delete(int id) {
        return menuMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteBatchByPKArr(int[] ids) {
        return menuMapper.deleteBatchByPKArr(ids);
    }

    @Override
    public List<Menu> list() {
        return menuMapper.selectAllMenu();
    }

    @Override
    public List<Menu> listSelect(Menu menu) {
        return menuMapper.selectEMenuSelective(menu);
    }

    @Override
    public int add(Menu menu) {
        return menuMapper.insertSelective(menu);
    }

    @Override
    public Menu getByName(String name) {
        Menu menu = new Menu();
        menu.setName(name);
        return menuMapper.selectEMenuSelective(menu).get(0);
    }

    @Override
    public int update(Menu menu) {
        return menuMapper.updateByPrimaryKeySelective(menu);
    }

    //调用Mapper的查询单个EMenu by roleId,如果有重复则去除.
    //最终返回角色list中涵盖的menu id list
    @Override
    public List<Integer> findMenuIdListByRoleIds(int[] roleIds) {
        List<Menu> resultList = new ArrayList<>();
        List<Integer> eMenuIdList = new LinkedList<>();
        Set<Menu> eMenuSet = new HashSet();
        for (int roleId : roleIds) {
            List<Menu> menuList = menuMapper.selectEMenuByRoleId(roleId);
            //多个List去重 放入menu的id而不是实例
            eMenuSet.addAll(menuList);
        }
        resultList.addAll(eMenuSet);
        //获取去重后的EMenu,将id放入eMenuIdList
        for (Menu menu : resultList) {
            eMenuIdList.add(menu.getId());
        }
        return eMenuIdList;
    }

    @Override
    public List<Menu> findMenuListByRoleId(int roleId) {
        return menuMapper.selectEMenuByRoleId(roleId);
    }

    @Override
    public List<Menu> findByParentId(int parentId) {
        return menuMapper.selectEMenuByParentId(parentId);
    }

    /*通过父节点和角色id数组获取EMenuList,已在SQL中进行了菜单去重*/
    @Override
    public List<Menu> findByParentIdAndRoleIdArr(int parentId, int[] ids) {
        List<Menu> eMenuList = menuMapper.selectEMenuByParentIdAndRoleIdArr(parentId, ids);
        return eMenuList;
    }

    @Override
    public List<Menu> findByParentIdAndEMenuIds(int parentId,int[] menuIds) {
        return menuMapper.selectEMenuByParentIdAndEMenuIds(parentId,menuIds);
    }

    @Override
    public List<Menu> findByParentIdAndSingleEMenuId(int parentId, int menuId) {
        return menuMapper.selectEMenuByParentIdAndSingleEMenuId(parentId,menuId);
    }

    @Override
    public List<Menu> findByParentIdAndSingleRoleId(int parentId, int roleId) {
        return menuMapper.selectEMenuByParentIdAndRoleId(parentId, roleId);
    }
}
